﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using NetDisk_WebService.Common;
using System.Data;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;
using System.ServiceModel.Activation;
using Newtonsoft.Json.Linq;
using NetDisk_WebService.Common.DEncrypt;
using System.Web.Security;

namespace NetDisk_WebService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“NetDiskService”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class NetDiskService : INetDiskService
    {

        private BLL.NetDisk netDiskBLL = new BLL.NetDisk();

        private IUniValidate uniValidate = new UniValidate();
        /// 测试数据库连接是否成功
        /// </summary>
        /// <returns></returns>
        public string DBConnectTest()
        {
            string returnMsg = "";
            int result = netDiskBLL.DBConnectTest();
            if (result == -1)
            {
                returnMsg = uniValidate.DbConnectError();
            }
            else
            {
                returnMsg = uniValidate.NoError();
            }
            return returnMsg;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="logID"></param>
        /// <returns></returns>
        public string Login(string loginString)
        {
            string returnMsg = "";
            string entityNo = GlobalConstant.ENTITYNO;
            bool isSuccess = false;
            //解密数据包
            try
            {
                loginString = DESEncrypt.Decrypt(loginString, Common.GlobalConstant.GLOBAL_ENCRYPTVALUE);
                JObject msgJson = JObject.Parse(loginString);
                string logid = msgJson["logid"].ToString();
                string password = msgJson["password"].ToString();

                bool hasPrvUser = netDiskBLL.Exists(logid, entityNo);
                if (hasPrvUser)
                {

#if DEBUG
                    //DEBUG模式
                    object mode = msgJson["mode"];
                    if (mode != null && mode.ToString() == "DEBUG")
                    {
                        string userName = netDiskBLL.GetUserFullname(logid, entityNo);
                        JObject jObject = new JObject();
                        jObject["FLogID"] = logid;
                        jObject["FUserName"] = userName;

                        returnMsg = uniValidate.NoError(jObject.ToString());
                        isSuccess = true;
                    }
                    else
                    {
                        returnMsg = uniValidate.BcsError();
                    }
#else
                     //验证密码
                    string dataPassword = netDiskBLL.GetUserPassword(logid, entityNo);
                    //string outString1 = Common.MD5.MD5Encrypt(entityNo + logid.ToLower() + password);
                    string outString = FormsAuthentication.HashPasswordForStoringInConfigFile(entityNo + logid + password, "MD5");
                    if (outString.ToLower() != dataPassword.ToLower())
                    {
                        returnMsg = uniValidate.UserPasswordError();
                    }
                    else
                    {

                        string userName = netDiskBLL.GetUserFullname(logid, entityNo);
                        JObject jObject = new JObject();
                        jObject["FLogID"] = logid;
                        jObject["FUserName"] = userName;

                        returnMsg = uniValidate.NoError(jObject.ToString());
                        isSuccess = true;
                    }
#endif
                    if (isSuccess)
                    {
                        OperateSession.add(GlobalConstant.PARAM_LOGINID, logid);//把登陆ID保存到Session
                    }

                }
                else
                {
                    returnMsg = uniValidate.UserNotExists();
                }



            }
            catch
            {
                returnMsg = uniValidate.ParamError();
            }
            return returnMsg;

        }
        /// <summary>
        /// 获取网盘版本号
        /// </summary>
        /// <returns></returns>
        public string GetFAppVer()
        {
            string returnMsg = "";
            string entityNo = GlobalConstant.ENTITYNO;
            string logid = OperateSession.get(GlobalConstant.PARAM_LOGINID);
            if (string.IsNullOrEmpty(logid))
            {
                returnMsg = uniValidate.UserIsNotLogin();
            }
            else
            {
                string strFAppVer = System.Configuration.ConfigurationManager.AppSettings["FAppVer"];

                if (string.IsNullOrEmpty(strFAppVer))
                {
                    returnMsg = uniValidate.BcsError();
                }
                else
                {
                    JObject jObject = new JObject();
                    jObject["FAppVer"] = strFAppVer;
                    returnMsg = uniValidate.NoError(jObject.ToString());
                }
            }
            return returnMsg;
        }
        /// <summary>
        /// 获取数据库服务器系统时间
        /// </summary>
        /// <returns></returns>
        public string GetServerDatetime()
        {
            string returnMsg = "";
            string logid = OperateSession.get(GlobalConstant.PARAM_LOGINID);
            if (string.IsNullOrEmpty(logid))
            {
                returnMsg = uniValidate.UserIsNotLogin();
            }
            else
            {
                JObject jObject = new JObject();
                jObject["ServerDatetime"] = netDiskBLL.GetServerDatetime().ToString("yyyy-MM-dd HH:mm:ss");
                returnMsg = uniValidate.NoError(jObject.ToString());
            }
            return returnMsg;
        }


        public string GetTempDir()
        {
            string returnMsg = "";
            string tempDir = System.Web.Configuration.WebConfigurationManager.AppSettings["TempDir"];
            if (string.IsNullOrEmpty(tempDir))
            {
                returnMsg = uniValidate.FileDoesNotExist();
            }
            else
            {
                JObject jObject = new JObject();
                jObject["TempDir"] = tempDir;
                returnMsg = uniValidate.NoError(jObject.ToString());
            }
            return returnMsg;
        }

        public string GetTempFileName(string FileName)
        {
            string returnMsg = "";
            string tempDir = System.Web.Configuration.WebConfigurationManager.AppSettings["TempDir"];
            if (string.IsNullOrEmpty(tempDir))
            {
                returnMsg = uniValidate.FileDoesNotExist();
            }
            else
            {
                JObject jObject = new JObject();
                jObject["TempFileName"] = tempDir + GenerateUtil.GenerateStringId() + "_" + FileName;
                returnMsg = uniValidate.NoError(jObject.ToString());
            }
            return returnMsg;
        }

        public string GetNetDiskDir()
        {
            return System.Web.Configuration.WebConfigurationManager.AppSettings["NetDiskDir"];
        }

        public string GetUploadPostUrl()
        {
            return System.Web.Configuration.WebConfigurationManager.AppSettings["uploadPostUrl"];
        }
        public string GetDownloadPostUrl()
        {
            return System.Web.Configuration.WebConfigurationManager.AppSettings["downloadPostUrl"];
        }

        public bool CreateDir(string path)
        {
            return serverIO.CreateDir(path);
        }

        public bool DelDir(string path)
        {
            return serverIO.DelDir(path);
        }

        public bool MoveDir(string sourcePath, string destPath)
        {
            return serverIO.MoveDir(sourcePath, destPath);
        }

        public bool DelFile(string path)
        {
            return serverIO.DelFile(path);
        }

        public bool MoveFile(string sourcePath, string destPath)
        {
            return serverIO.MoveFile(sourcePath, destPath);
        }

        public bool CopyFile(string sourcePath, string destPath)
        {
            return serverIO.CopyFile(sourcePath, destPath);
        }

        public bool ExistDir(string path)
        {
            return serverIO.ExistDir(path);
        }
        public bool ExistFile(string path)
        {
            return serverIO.ExistFile(path);
        }

        public string AddNetdisk(string jsonNetDiskModel)
        {
            Model.NetDisk netDiskModel = JsonConvert.DeserializeObject<Model.NetDisk>(jsonNetDiskModel);
            netDiskModel.FEntityNo = GlobalConstant.ENTITYNO;
            decimal FID = netDiskBLL.AddNetDisk(netDiskModel);
            return FID.ToString();
        }

        public string AddNetdiskCover(string jsonNetDiskModel)
        {
            Model.NetDisk netDiskModel = JsonConvert.DeserializeObject<Model.NetDisk>(jsonNetDiskModel);
            netDiskModel.FEntityNo = GlobalConstant.ENTITYNO;
            decimal FID = netDiskBLL.AddNetDiskCover(netDiskModel);
            return FID.ToString();
        }

        public string GetList(string FParentID, string orderField)
        {
            string entityNo = GlobalConstant.ENTITYNO;
            List<Model.NetDisk> netDiskList = netDiskBLL.GetList(decimal.Parse(FParentID), entityNo, orderField);
            return JsonConvert.SerializeObject(netDiskList);
        }

        public string GetListCTE(string FParentID, string strWhere, string orderField)
        {
            string entityNo = GlobalConstant.ENTITYNO;
            List<Model.NetDisk> netDiskList = netDiskBLL.GetListCTE(decimal.Parse(FParentID), entityNo, strWhere, orderField);
            foreach (Model.NetDisk netDiskModel in netDiskList)
            {
                netDiskModel.relPath = GetPathByPid(netDiskModel.FID.ToString());
            }
            return JsonConvert.SerializeObject(netDiskList);
        }

        public string GetDirByLogID(string logID)
        {
            string entityNo = GlobalConstant.ENTITYNO;
            List<Model.NetDisk> netDiskList = netDiskBLL.GetDirByFLogID(entityNo, logID);
            return JsonConvert.SerializeObject(netDiskList);
        }


        public string GetSameFID(string FParentID, string FName)
        {
            string entityNo = GlobalConstant.ENTITYNO;
            decimal FID = netDiskBLL.GetSame(decimal.Parse(FParentID), FName, entityNo);
            return FID.ToString();
        }

        public string DelNetDisk(string strIds)
        {
            string returnMsg = "";
            bool isSuccess = true;
            string entityNo = Common.GlobalConstant.ENTITYNO;

            //获取id
            strIds = strIds.Trim(',');
            string strWhere = " FID IN (" + strIds + ") ";
            List<Model.NetDisk> netDiskList = netDiskBLL.GetModelList(strWhere, entityNo);

            //先判断是否有子文件的文件夹
            List<Model.NetDisk> DirectoryHaveChildList = netDiskBLL.GetDirectoryHaveChild(entityNo, strIds);
            if (DirectoryHaveChildList.Count > 0)
            {
                string Directorys = "";
                foreach (Model.NetDisk netDiskObject in DirectoryHaveChildList)
                {
                    Directorys += netDiskObject.FName + ",";
                }
                Directorys = Directorys.Trim(',');

                isSuccess = false;
                string message = "{'code':'31070','msg':'文件删除失败,【" + Directorys + "】文件夹包含文件或子文件夹，不能删除。'}";
                returnMsg = uniValidate.FileDeleteFailed(message);
            }
            else//如果没有子文件夹
            {

                foreach (Model.NetDisk netDiskModel in netDiskList)
                {
                    isSuccess = DeletePrivate(netDiskModel.FID.ToString());
                }

                if (isSuccess)
                {
                    returnMsg = uniValidate.NoError();
                }
                else
                {
                    returnMsg = uniValidate.FileDeleteFailed();
                }
            }
            return returnMsg;
        }

        public string CreateDirNetDisk(string jsonNetDiskModel)
        {
            string returnMsg = "";
            bool isSuccess = true;
            decimal FID = 0;
            string entityNo = Common.GlobalConstant.ENTITYNO;

            Model.NetDisk netDiskModel = JsonConvert.DeserializeObject<Model.NetDisk>(jsonNetDiskModel);
            netDiskModel.FEntityNo = GlobalConstant.ENTITYNO;

            //如果有相同的文件
            if (netDiskBLL.GetSame(netDiskModel.FParentID.Value, netDiskModel.FName, entityNo) != 0)
            {
                isSuccess = false;
                returnMsg = uniValidate.FileAlreadyExists();
            }

            if (isSuccess)
            {
                try
                {
                    string srvPath = System.Configuration.ConfigurationManager.AppSettings["NetDiskDir"];
                    srvPath += GetPathByPid(netDiskModel.FParentID.Value.ToString()) + "\\";//获取父文件夹路径
                    //先产生文件夹
                    srvPath += netDiskModel.FName;
                    CreateDir(srvPath);
                    isSuccess = true;
                }
                catch
                {
                    isSuccess = false;
                }
            }
            if (isSuccess)
            {
                try
                {
                    //再插入数据
                    FID = netDiskBLL.AddNetDisk(netDiskModel);
                    JObject jObject = new JObject();
                    jObject["FID"] = FID;
                    returnMsg = uniValidate.NoError(jObject.ToString());
                }
                catch
                {
                    returnMsg = uniValidate.FileCreateFailed();
                }
            }
            return returnMsg;
        }


        public string CopyNetDisk(string strIds, string destDirId)
        {
            string returnMsg = "";
            string entityNo = Common.GlobalConstant.ENTITYNO;
            string NetDiskDir = System.Configuration.ConfigurationManager.AppSettings["NetDiskDir"];

            strIds = strIds.Trim(',');
            string strWhere = " FID IN (" + strIds + ") ";
            List<Model.NetDisk> netDiskList = netDiskBLL.GetModelList(strWhere, entityNo);

            Model.NetDisk destDirModel = netDiskBLL.GetModel(decimal.Parse(destDirId));

            if (destDirModel != null)
            {
                foreach (Model.NetDisk netDiskModel in netDiskList)
                {
                    bool isSuccess = true;

                    //判断文件是否存在
                    if (isSuccess)
                    {
                        Model.NetDisk tempNetDiskModel = netDiskBLL.GetModel(netDiskModel.FID);
                        if (tempNetDiskModel == null)
                        {
                            isSuccess = false;
                        }
                    }

                    //先复制文件
                    string StrPath = "";
                    string DestPath = "";

                    if (isSuccess)
                    {
                        StrPath = NetDiskDir + GetPathByPid(netDiskModel.FID.ToString());
                        DestPath = NetDiskDir + GetPathByPid(destDirId) + "\\" + netDiskModel.FName;
                    }

                    //如果有相同的文件
                    if (netDiskBLL.GetSame(decimal.Parse(destDirId), netDiskModel.FName, entityNo) != 0)
                    {
                        isSuccess = false;
                        returnMsg = uniValidate.FileAlreadyExists();
                    }

                    if (isSuccess)
                    {
                        try
                        {
                            if (netDiskModel.FType == "D")
                            {
                                //文件夹不作复制功能。                  
                            }
                            else
                            {
                                CopyFile(StrPath, DestPath);
                            }
                            isSuccess = true;
                        }
                        catch
                        {
                            isSuccess = false;
                        }
                    }
                    if (isSuccess)
                    {
                        try
                        {
                            if (netDiskModel.FType == "D")
                            {
                                //文件夹不作复制功能。   
                                returnMsg = uniValidate.DirectorCanNotCopyLimit();
                            }
                            else
                            {
                                //再修改数据
                                Model.NetDisk newNetDiskModel = netDiskModel;
                                newNetDiskModel.FParentID = decimal.Parse(destDirId);
                                netDiskBLL.AddNetDisk(newNetDiskModel);
                                returnMsg = uniValidate.NoError();
                            }
                        }
                        catch
                        {
                            returnMsg = uniValidate.FileCopyFailed();
                        }
                    }

                }
            }
            else
            {
                returnMsg = uniValidate.FileParentPathDoesNotExist();
            }

            return returnMsg;
        }


        public string MoveNetDisk(string strIds, string destDirId)
        {
            string returnMsg = "";
            string entityNo = Common.GlobalConstant.ENTITYNO;
            string NetDiskDir = System.Configuration.ConfigurationManager.AppSettings["NetDiskDir"];

            strIds = strIds.Trim(',');
            string strWhere = " FID IN (" + strIds + ") ";
            List<Model.NetDisk> netDiskList = netDiskBLL.GetModelList(strWhere, entityNo);
            List<Model.NetDisk> netDiskDirList = netDiskList.Where(h => h.FType == "D").ToList();
            foreach (Model.NetDisk netDiskModel in netDiskDirList)
            {
                if (netDiskBLL.IsErrorMoveParentDirChildDir(netDiskModel.FID, decimal.Parse(destDirId), entityNo))
                {
                    returnMsg = uniValidate.FileMoveFailed();
                    return returnMsg;
                }

            }
            Model.NetDisk destDirModel = netDiskBLL.GetModel(decimal.Parse(destDirId));

            if (destDirModel != null)
            {
                foreach (Model.NetDisk netDiskModel in netDiskList)
                {
                    bool isSuccess = true;

                    if (isSuccess)
                    {
                        Model.NetDisk tempNetDiskModel = netDiskBLL.GetModel(netDiskModel.FID);
                        if (tempNetDiskModel == null)
                        {
                            isSuccess = false;
                        }
                    }
                    //先移动文件
                    string StrPath = "";
                    string DestPath = "";
                    if (isSuccess)
                    {
                        StrPath = NetDiskDir + GetPathByPid(netDiskModel.FID.ToString());
                        DestPath = NetDiskDir + GetPathByPid(destDirId) + "\\" + netDiskModel.FName;
                    }
                    //如果有相同的文件
                    if (netDiskBLL.GetSame(decimal.Parse(destDirId), netDiskModel.FName, entityNo) != 0)
                    {
                        isSuccess = false;
                        returnMsg = uniValidate.FileAlreadyExists();
                    }

                    if (isSuccess)
                    {
                        try
                        {
                            if (netDiskModel.FType == "D")
                            {
                                MoveDir(StrPath, DestPath);
                            }
                            else
                            {
                                MoveFile(StrPath, DestPath);
                            }
                            isSuccess = true;
                        }
                        catch
                        {
                            isSuccess = false;
                        }
                    }
                    if (isSuccess)
                    {
                        try
                        {
                            //再修改数据
                            netDiskBLL.moveNetDisk(netDiskModel.FID, decimal.Parse(destDirId));
                            returnMsg = uniValidate.NoError();
                        }
                        catch
                        {
                            returnMsg = uniValidate.FileMoveFailed();
                        }
                    }
                }
            }
            else
            {
                returnMsg = uniValidate.FileParentPathDoesNotExist();
            }
            return returnMsg;
        }

        public string RenameNetDisk(string FID, string FName)
        {
            string returnMsg = "";
            bool isSuccess = true;
            string entityNo = Common.GlobalConstant.ENTITYNO;
            string NetDiskDir = System.Configuration.ConfigurationManager.AppSettings["NetDiskDir"];

            string oldFileName = "";
            string newFileName = "";
            Model.NetDisk netDiskModel = netDiskBLL.GetModel(decimal.Parse(FID));

            //如果有相同的文件
            if (netDiskBLL.GetSame(netDiskModel.FParentID.Value, FName, entityNo, netDiskModel.FID.ToString()) != 0)
            {
                isSuccess = false;
                returnMsg = uniValidate.FileAlreadyExists();
            }

            //先修改物理文件名
            if (isSuccess)
            {
                try
                {
                    string path = NetDiskDir + GetPathByPid(netDiskModel.FParentID.Value.ToString()) + "\\";
                    oldFileName = path + netDiskModel.FName;
                    newFileName = FName;
                    if (netDiskModel.FType == "F")
                    {
                        if (!string.IsNullOrEmpty(netDiskModel.FSuffixName))//如果后缀名不为空，则加入
                        {
                            newFileName += "." + netDiskModel.FSuffixName;
                        }
                    }
                    if (netDiskModel.FType == "D")
                    {
                        MoveDir(oldFileName, path + newFileName);
                    }
                    else
                    {
                        MoveFile(oldFileName, path + newFileName);
                    }
                }
                catch
                {
                    isSuccess = false;
                    returnMsg = uniValidate.FileRenameFailed();
                }
            }

            if (isSuccess)
            {
                try
                {

                    netDiskBLL.renameNetDisk(decimal.Parse(FID), newFileName);
                    returnMsg = uniValidate.NoError();
                }
                catch
                {
                    isSuccess = false;
                    returnMsg = uniValidate.FileRenameFailed();
                }
            }
            return returnMsg;



        }

        /// <summary>
        /// 获取当前id的路径
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public string GetPathByPid(string pId)
        {
            DataTable dtPreNetDisk = netDiskBLL.GetListPre(decimal.Parse(pId));
            string strPreNetDisk = "";
            foreach (DataRow row in dtPreNetDisk.Rows)
            {
                strPreNetDisk += row["FName"].ToString() + "\\";
            }
            strPreNetDisk = strPreNetDisk.Trim('\\');
            return strPreNetDisk;
        }

        /// <summary>
        /// 生成下载文件的压缩包
        /// </summary>
        /// <param name="strIds"></param>
        /// <param name="FParentID"></param>
        /// <returns></returns>
        public string DownloadZip(string strIds)
        {
            string returnMsg = "";
            bool isSuccess = true;
            string entityNo = Common.GlobalConstant.ENTITYNO;
            string zipPath = "";

            try
            {
                string NetDiskDir = System.Configuration.ConfigurationManager.AppSettings["NetDiskDir"];
                string TempDir = System.Configuration.ConfigurationManager.AppSettings["TempDir"];
                TempDir += "NetDiskDownload\\";
                CreateDir(TempDir);//生成下载临时文件夹
                strIds = strIds.Trim(',');
                string strWhere = " FID IN (" + strIds + ") ";
                List<Model.NetDisk> netDiskList = netDiskBLL.GetModelList(strWhere, entityNo);

                zipPath = TempDir + DateTime.Now.ToString("yyyyMMddHHmm") + Common.GenerateUtil.GenerateStringId() + ".zip";//zip文件路径
                string FParentID = netDiskList[0].FParentID.ToString();//设置父路径
                string zipTopDirectoryPath = NetDiskDir + GetPathByPid(FParentID) + "\\";//父级文件夹路径

                var filesOrDirectoriesPaths = new List<string>();
                foreach (Model.NetDisk netDiskModel in netDiskList)
                {
                    filesOrDirectoriesPaths.Add(zipTopDirectoryPath + netDiskModel.FName);
                }
                Zip.CreateZip(zipPath, zipTopDirectoryPath, 6, "", filesOrDirectoriesPaths, true);
            }
            catch
            {
                isSuccess = false;
            }
            if (isSuccess)
            {
                JObject jObject = new JObject();
                jObject["zipPath"] = zipPath;
                returnMsg = uniValidate.NoError(jObject.ToString());
            }
            else
            {
                returnMsg = uniValidate.FileCreateFailed();
            }
            return returnMsg;
        }

        /// <summary>
        /// 初始文件夹
        /// </summary>
        public string InitMyDirs()
        {
            string returnMsg = "";
            bool isSuccess = true;
            decimal FID = 0;
            string entityNo = Common.GlobalConstant.ENTITYNO;
            string NetDiskDir = System.Configuration.ConfigurationManager.AppSettings["NetDiskDir"];
            string initDirs = System.Configuration.ConfigurationManager.AppSettings["initDirs"];
            List<string> initDirList = initDirs.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();

            //先创建根目录
            string logId = OperateSession.get(GlobalConstant.PARAM_LOGINID);

            Model.NetDisk netDiskModel = new Model.NetDisk();


            //先检查是否存在根目录
            Model.NetDisk rootNetDisk = netDiskBLL.GetRoot(logId, entityNo);

            if (rootNetDisk == null)
            {
                if (isSuccess)
                {
                    try
                    {
                        //根目录
                        netDiskModel = new Model.NetDisk();
                        netDiskModel.FParentID = null;
                        netDiskModel.FEntityNo = entityNo;
                        netDiskModel.FName = logId;
                        netDiskModel.FSuffixName = null;
                        netDiskModel.FType = "D";
                        netDiskModel.FSize = 0;
                        netDiskModel.FLogID = logId;
                        netDiskModel.FMD5 = null;

                        FID = decimal.Parse(CreateDirPrivate(netDiskModel));
                        isSuccess = true;
                    }
                    catch
                    {
                        isSuccess = false;
                    }
                }
                if (isSuccess)
                {
                    try
                    {
                        if (FID != 0)
                        {
                            foreach (string initDir in initDirList)
                            {
                                netDiskModel = new Model.NetDisk();
                                netDiskModel.FParentID = FID;
                                netDiskModel.FEntityNo = entityNo;
                                netDiskModel.FName = initDir;
                                netDiskModel.FSuffixName = null;
                                netDiskModel.FType = "D";
                                netDiskModel.FSize = 0;
                                netDiskModel.FLogID = logId;
                                netDiskModel.FMD5 = null;

                                CreateDirPrivate(netDiskModel);
                            }
                        }
                        isSuccess = true;
                    }
                    catch
                    {
                        isSuccess = false;
                    }
                }
            }
            else
            {
                JObject jObject = new JObject();
                jObject["FID"] = rootNetDisk.FID;
                returnMsg = uniValidate.NoError(jObject.ToString());
            }

            if (isSuccess)
            {
                JObject jObject = new JObject();
                jObject["FID"] = FID;
                returnMsg = uniValidate.NoError(jObject.ToString());
            }
            else
            {
                returnMsg = uniValidate.FileCreateFailed();
            }
            return returnMsg;
        }

        public string GetNetDisk(string FID)
        {
            string returnMsg = "";
            string JsonNetDiskModel = "";
            try
            {
                Model.NetDisk netdiskModel = netDiskBLL.GetModel(decimal.Parse(FID));
                if (netdiskModel != null)
                {
                    JsonNetDiskModel = JsonConvert.SerializeObject(netdiskModel);
                }
                JObject jObject = new JObject();
                jObject["JsonNetDiskModel"] = JsonNetDiskModel;
                returnMsg = uniValidate.NoError(jObject.ToString());
            }
            catch
            {
                returnMsg = uniValidate.DbQueryError();
            }
            return returnMsg;
        }




        #region 私有函数

        private bool DeletePrivate(string id)
        {
            bool isSuccess = true;
            string entityNo = Common.GlobalConstant.ENTITYNO;
            //先删除子级
            List<Model.NetDisk> ChildNetDiskList = netDiskBLL.GetList(decimal.Parse(id), entityNo, "");
            if (ChildNetDiskList.Count > 0)
            {
                foreach (Model.NetDisk ChildNetDiskModel in ChildNetDiskList)
                {
                    DeletePrivate(ChildNetDiskModel.FID.ToString());
                }
            }
            //删除文件
            Model.NetDisk netDiskModel = netDiskBLL.GetModel(decimal.Parse(id));
            if (isSuccess)
            {
                try
                {
                    string srvPath = System.Configuration.ConfigurationManager.AppSettings["NetDiskDir"];
                    if (netDiskModel.FParentID != null)
                    {
                        srvPath += GetPathByPid(netDiskModel.FParentID.Value.ToString()) + "\\";
                    }
                    if (netDiskModel.FType == "D")
                    {
                        DelDir(srvPath + netDiskModel.FName);
                    }
                    else
                    {
                        DelFile(srvPath + netDiskModel.FName);
                    }
                    isSuccess = true;
                }
                catch
                {
                    isSuccess = false;
                }
            }
            //如果文件删除成功，再删除数据
            try
            {
                if (isSuccess)
                {
                    //删除文件，同时删除所有下级文件
                    netDiskBLL.deleteNetDisk(netDiskModel.FID, entityNo);
                }
                isSuccess = true;
            }
            catch
            {
                isSuccess = false;
            }
            return isSuccess;
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="netDiskModel"></param>
        /// <returns></returns>
        private string CreateDirPrivate(Model.NetDisk netDiskModel)
        {
            bool isSuccess = true;
            decimal FID = 0;

            if (isSuccess)
            {
                try
                {
                    string srvPath = System.Configuration.ConfigurationManager.AppSettings["NetDiskDir"];
                    if (netDiskModel.FParentID != null)
                    {
                        srvPath += GetPathByPid(netDiskModel.FParentID.Value.ToString()) + "\\";//获取父文件夹路径
                    }
                    //先产生文件夹
                    srvPath += netDiskModel.FName;
                    CreateDir(srvPath);
                    isSuccess = true;
                }
                catch
                {
                    isSuccess = false;
                }
            }
            if (isSuccess)
            {
                try
                {
                    //再插入数据
                    FID = netDiskBLL.AddNetDisk(netDiskModel);
                    isSuccess = true;
                }
                catch
                {
                    isSuccess = false;
                }
            }
            return FID.ToString();
        }


        #endregion



    }
}
